<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>消息队列核心原理解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .number-circle {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            color: white;
            font-weight: bold;
            flex-shrink: 0;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
            border-left: 4px solid #667eea;
            padding: 1.5rem;
            border-radius: 8px;
            margin: 1.5rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">消息队列核心原理</h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">深入解析现代分布式系统的基石</p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-layer-group mr-2"></i>
                        <span>异步通信</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>高可靠性</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-expand-arrows-alt mr-2"></i>
                        <span>可扩展性</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6 max-w-4xl">
            <div class="prose prose-lg">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">消</span>息队列（Message Queue, MQ）是一种用于异步通信的中间件，允许系统之间或系统内部组件之间传递消息。MQ 的核心原理围绕如何有效地发送、接收、存储和处理消息。确保了系统的可靠性、可扩展性和高效性。每种消息队列实现可能会在这些核心原理的基础上增加特定的功能和优化，适应不同的应用场景和需求。
                </p>
            </div>
        </div>
    </section>

    <!-- Basic Concepts Section -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">基本概念</h2>
            <div class="grid md:grid-cols-2 lg:grid-cols-4 gap-6">
                <div class="bg-white p-6 rounded-lg card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-envelope"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">消息</h3>
                    <p class="text-gray-600">消息是一个数据单元，通常包含业务数据和一些元数据（如消息头）。消息可以是文本、JSON、XML 或二进制数据。</p>
                </div>
                <div class="bg-white p-6 rounded-lg card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-list"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">队列</h3>
                    <p class="text-gray-600">队列是存储消息的容器。消息在队列中按顺序排列，消费者从队列中获取消息进行处理。</p>
                </div>
                <div class="bg-white p-6 rounded-lg card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-paper-plane"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">生产者</h3>
                    <p class="text-gray-600">生产者是发送消息到队列的组件或应用程序。</p>
                </div>
                <div class="bg-white p-6 rounded-lg card-hover">
                    <div class="text-4xl mb-4 feature-icon">
                        <i class="fas fa-user-check"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">消费者</h3>
                    <p class="text-gray-600">消费者是从队列中接收和处理消息的组件或应用程序。</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Message Lifecycle Section -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">消息的生命周期</h2>
            <div class="max-w-4xl mx-auto">
                <div class="space-y-6">
                    <div class="flex items-start space-x-4">
                        <div class="number-circle">1</div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2">消息生成</h3>
                            <p class="text-gray-600">生产者生成消息并将其发送到消息队列。消息通常包含业务数据和一些元数据（如消息 ID、时间戳、优先级等）。</p>
                        </div>
                    </div>
                    <div class="flex items-start space-x-4">
                        <div class="number-circle">2</div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2">消息存储</h3>
                            <p class="text-gray-600">消息队列将接收到的消息存储在内部的存储系统中。这可能是内存、磁盘或数据库，具体取决于消息队列的实现。</p>
                        </div>
                    </div>
                    <div class="flex items-start space-x-4">
                        <div class="number-circle">3</div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2">消息传递</h3>
                            <p class="text-gray-600">消息队列负责将消息从生产者传递到消费者。这可能涉及到将消息存储在队列中，等待消费者的请求。</p>
                        </div>
                    </div>
                    <div class="flex items-start space-x-4">
                        <div class="number-circle">4</div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2">消息消费</h3>
                            <p class="text-gray-600">消费者从队列中获取消息并进行处理。处理完成后，消费者通常会确认消息（acknowledge），以表示消息已经成功处理。</p>
                        </div>
                    </div>
                    <div class="flex items-start space-x-4">
                        <div class="number-circle">5</div>
                        <div class="flex-1">
                            <h3 class="text-xl font-bold mb-2">消息删除</h3>
                            <p class="text-gray-600">一旦消息被成功处理并确认，消息队列将从队列中删除消息。未被处理的消息可能会在队列中保留，直到超时或被重新投递。</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Visualization Section -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">消息队列架构图</h2>
            <div class="max-w-6xl mx-auto">
                <div class="mermaid">
                    graph LR
                        A[生产者] -->|发送消息| B[消息队列]
                        B -->|存储| C[持久化存储]
                        B -->|分发| D[消费者1]
                        B -->|分发| E[消费者2]
                        B -->|分发| F[消费者3]
                        D -->|确认| B
                        E -->|确认| B
                        F -->|确认| B
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Core Principles Section -->
    <section class="py-16 bg-white">
        <div class="container mx-auto px-6">
            <h2 class="text-3xl font-bold mb-12 text-center section-title">核心原理详解</h2>
            <div class="grid md:grid-cols-2 gap-8 max-w-6xl mx-auto">
                <!-- 消息存储和持久化 -->
                <div class="highlight-box">
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <i class="fas fa-database mr-3 text-purple-600"></i>
                        消息存储和持久化
                    </h3>
                    <div class="space-y-3">
                        <div>
                            <h4 class="font-bold text-lg mb-2">持久化</h4>
                            <p class="text-gray-600">为了确保消息不丢失，消息队列通常会将消息持久化到磁盘。这种持久化机制使消息在系统重启或故障后能够恢复。</p>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">日志文件</h4>
                            <p class="text-gray-600">许多消息队列系统使用日志文件来存储消息，这些文件按照时间顺序排列，确保消息的顺序和一致性。</p>
                        </div